package top.skihub.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.skihub.pojo.MineInfo;
import top.skihub.pojo.Result;
import top.skihub.service.MineInfoService;

import java.util.List;

/**
 * 添加个人信息
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class MineInfoController {
    @Value("${server.port}")
    private String port;
    @Autowired
    private MineInfoService mineInfoService; // 注入服务实例
    public MineInfoController(MineInfoService mineInfoService) {
        this.mineInfoService = mineInfoService;
    }

    @PostMapping("/addmineinfo")
    public Result addMineInfo(@RequestBody MineInfo mineInfo)
    {
        try {
            log.info("服务端口: {}, 注册个人信息:{}", port, mineInfo);
            mineInfoService.addMineInfo(mineInfo);
            return Result.success(mineInfo);
        } catch (Exception e) {
            log.error("服务端口: {}, 注册个人信息失败", port, e);
            return Result.error("添加个人信息失败");
        }
    }

    @GetMapping("/getallmineinfo/{phone}/{passwd}")
    public Result listAllMineInfo(@PathVariable String phone,
                                  @PathVariable String passwd) {
        try {
            List<MineInfo> allInfoList = mineInfoService.listAllMineInfo(phone, passwd);
            log.info("服务端口: {}, 获取 {} 的个人信息: {}", port, phone, allInfoList);
            
            if (allInfoList != null && !allInfoList.isEmpty()) {
                MineInfo userInfo = allInfoList.get(0);
                if (userInfo.getUserId() == null) {
                    log.error("用户信息不完整: userId 为空");
                    return Result.error("用户信息不完整");
                }
                return Result.success(userInfo);
            } else {
                return Result.error("请仔细核对您的账号和密码");
            }
        } catch (Exception e) {
            log.error("服务端口: {}, 获取 {} 的个人信息失败", port, phone, e);
            return Result.error("获取个人信息失败");
        }
    }

    @GetMapping("/getmineinfo/{email}")
    public Result list(@PathVariable String email) {
        try {
            log.info("服务端口: {}, 获取个人信息", port);
            List<MineInfo> mineInfoList = mineInfoService.list(email);
            if (mineInfoList != null && !mineInfoList.isEmpty()) {
                MineInfo userInfo = mineInfoList.get(0);
                if (userInfo.getUserId() == null) {
                    log.error("用户信息不完整: userId 为空");
                    return Result.error("用户信息不完整");
                }
                return Result.success(userInfo);
            }
            return Result.success(mineInfoList);
        } catch (Exception e) {
            log.error("服务端口: {}, 获取个人信息失败", port, e);
            return Result.error("获取个人信息失败");
        }
    }

    @PutMapping("/updateInfo")
    public Result updateInfo(@RequestBody MineInfo mineInfo){
        try {
            if (mineInfo.getUserId() == null) {
                log.error("服务端口: {}, 更新失败: userId 为空", port);
                return Result.error("用户信息不完整");
            }
            log.info("服务端口: {}, 更新个人信息：{}", port, mineInfo);
            mineInfoService.updateMineInfo(mineInfo);
            return Result.success();
        } catch (Exception e) {
            log.error("服务端口: {}, 更新个人信息失败", port, e);
            return Result.error("更新个人信息失败");
        }
    }
}
